package com.sandking.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.io.SK_OutputStream;
import java.util.List;
import com.sandking.io.SK_InputStream;
import com.sandking.db.cache.XxzxbbCache;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;

/**
 * 详细在线报表
 */
public class Xxzxbb {

	public static final String TABLENAME = "详细在线报表";
	public static final String CLASSNAME = "Xxzxbb"; 
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/** id */
	private int id;
	
	/** 服务器id */
	private int fwqid;
	
	/** 服务器名称 */
	private String fwqmc;
	
	/** 渠道 */
	private String qd;
	
	/** 时间 */
	private java.util.Date sj;
	
	
	public Xxzxbb() {
		super();
	}
	
	public Xxzxbb(int id, int fwqid, String fwqmc, String qd, java.util.Date sj) {
		super();
		this.id = id;
		this.fwqid = fwqid;
		this.fwqmc = fwqmc;
		this.qd = qd;
		this.sj = sj;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
		addUpdateColumn("id",id);
	}
	
	public void changeIdWith(int id){
		this.id += id;
		setId(this.id);
	}
	
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}	
	public int getFwqid() {
		return fwqid;
	}
	
	public void setFwqid(int fwqid) {
		this.fwqid = fwqid;
		addUpdateColumn("服务器id",fwqid);
	}
	
	public void changeFwqidWith(int fwqid){
		this.fwqid += fwqid;
		setFwqid(this.fwqid);
	}
	
	public void changeFwqidWithMin(int fwqid,int min){
		this.fwqid += fwqid;
		this.fwqid = this.fwqid < min ? min : this.fwqid;
		setFwqid(this.fwqid);
	}
	
	public void changeFwqidWithMax(int fwqid,int max){
		this.fwqid += fwqid;
		this.fwqid = this.fwqid > max ? max : this.fwqid;
		setFwqid(this.fwqid);
	}
	
	public void changeFwqidWithMaxMin(int fwqid,int max,int min){
		this.fwqid += fwqid;
		this.fwqid = this.fwqid < min ? min : this.fwqid;
		this.fwqid = this.fwqid > max ? max : this.fwqid;
		setFwqid(this.fwqid);
	}	
	public String getFwqmc() {
		return fwqmc;
	}
	
	public void setFwqmc(String fwqmc) {
		this.fwqmc = fwqmc;
		addUpdateColumn("服务器名称",fwqmc);
	}
	
	public String getQd() {
		return qd;
	}
	
	public void setQd(String qd) {
		this.qd = qd;
		addUpdateColumn("渠道",qd);
	}
	
	public java.util.Date getSj() {
		return sj;
	}
	
	public void setSj(java.util.Date sj) {
		this.sj = sj;
		addUpdateColumn("时间",sj);
	}
	
	
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("fwqid", this.fwqid);
        result.put("fwqmc", this.fwqmc);
        result.put("qd", this.qd);
        result.put("sj", this.sj);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("服务器id", this.fwqid);
        result.put("服务器名称", this.fwqmc);
        result.put("渠道", this.qd);
        result.put("时间", this.sj);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
		    SK_OutputStream.writeInt(out,this.id);
		    SK_OutputStream.writeInt(out,this.fwqid);
		    SK_OutputStream.writeString(out,this.fwqmc);
		    SK_OutputStream.writeString(out,this.qd);
		    return out.toByteArray();
    	}catch (Exception e) {
            throw e;
        }
    }
    
     public static Xxzxbb createForBytes(byte[] _byte) throws Exception {
     	try (ByteArrayInputStream in = new ByteArrayInputStream(_byte);) {
	     	Xxzxbb xxzxbb = new Xxzxbb();
		    xxzxbb.id = SK_InputStream.readInt(in,null);
		    xxzxbb.fwqid = SK_InputStream.readInt(in,null);
		    xxzxbb.fwqmc = SK_InputStream.readString(in,null);
		    xxzxbb.qd = SK_InputStream.readString(in,null);
		    return xxzxbb;
    	}catch (Exception e) {
            throw e;
        }
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Xxzxbb> createForColumnNameList(List<Map<String, Object>> list){
    	List<Xxzxbb> xxzxbbs = new ArrayList<Xxzxbb>();
		for (Map<String, Object> map : list) {
			xxzxbbs.add(createForColumnNameMap(map));
		}
		return xxzxbbs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Xxzxbb createForColumnNameMap(Map<String, Object> map){
    	Xxzxbb obj = new Xxzxbb();
	    obj.id = SK_Map.getInt("id", map);
	    obj.fwqid = SK_Map.getInt("服务器id", map);
	    obj.fwqmc = SK_Map.getString("服务器名称", map);
	    obj.qd = SK_Map.getString("渠道", map);
        return obj;
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Xxzxbb> createForList(List<Map<String, Object>> list){
    	List<Xxzxbb> xxzxbbs = new ArrayList<Xxzxbb>();
		for (Map<String, Object> map : list) {
			xxzxbbs.add(createForColumnNameMap(map));
		}
		return xxzxbbs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Xxzxbb createForMap(Map<String, Object> map){
    	Xxzxbb obj = new Xxzxbb();
	    obj.id = SK_Map.getInt("id", map);
	    obj.fwqid = SK_Map.getInt("fwqid", map);
	    obj.fwqmc = SK_Map.getString("fwqmc", map);
	    obj.qd = SK_Map.getString("qd", map);
        return obj;
    }
    
    public static Xxzxbb createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Xxzxbb> createForJson(List<String> jsons){
    	List<Xxzxbb> xxzxbbs = new ArrayList<Xxzxbb>();
    	for(String json : jsons){
    		xxzxbbs.add(createForJson(json));
    	}
    	return xxzxbbs;
    }
    
    /** 延迟插入数据库 */
    public Xxzxbb insert(){
    	return XxzxbbCache.insert(this);
    }
    /** 延迟更新数据库 */
    public Xxzxbb update(){
    	return XxzxbbCache.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return XxzxbbCache.delete(this);
    }
    /** 即时插入数据库 */
    public Xxzxbb insertAndFlush(){
    	return XxzxbbCache.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Xxzxbb updateAndFlush(){
    	return XxzxbbCache.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return XxzxbbCache.deleteAndFlush(this);
    }
}